package com.xiezc.util;

import com.alibaba.fastjson.JSON;
import okhttp3.*;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * Created by xiezc on 17/2/24.
 */
public class HttpUtil {

    static OkHttpClient client = new OkHttpClient();
    static final MediaType JSONCONTENTTYPE
            = MediaType.parse("application/json; charset=utf-8");

    /**
     * 发送http的get请求
     *
     * @param url
     * @throws IOException
     */
    public static Response get(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = client.newCall(request);

        return call.execute();
    }

    /**
     * http 的post请求. 请求的数据是json格式的"application/json; charset=utf-8"
     *
     * @param url
     * @param json
     * @return
     * @throws IOException
     */
    public static Response post(String url, JSON json) throws IOException {
        RequestBody requestBody = RequestBody.create(JSONCONTENTTYPE, json.toJSONString());
        Request request = new Request.Builder().url(url).post(requestBody).build();
        return client.newCall(request).execute();
    }


}
